package com.csust.automaticteach.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.automaticteach.constant.OrderConstant;
import com.csust.automaticteach.model.dto.promptself.PromptSelfQueryRequest;
import com.csust.automaticteach.model.entity.PromptSelf;
import com.csust.automaticteach.model.vo.promptself.PromptSelfVO;
import com.csust.automaticteach.service.PromptSelfService;
import com.csust.automaticteach.mapper.PromptSelfMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 钰玟
* @description 针对表【t_prompt_self(提示词表)】的数据库操作Service实现
* @createDate 2025-07-10 20:29:20
*/
@Service
public class PromptSelfServiceImpl extends ServiceImpl<PromptSelfMapper, PromptSelf>
    implements PromptSelfService{
    @Override
    public QueryWrapper<PromptSelf> getPromptSelfVOQueryWrapper(PromptSelfQueryRequest promptSelfQueryRequest) {
        //提取查询字段
        Long id = promptSelfQueryRequest.getId();
        String sortField = promptSelfQueryRequest.getSortField();
        String sortOrder = promptSelfQueryRequest.getSortOrder();
        //构造queryMapper
        QueryWrapper<PromptSelf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq("status", 1);
        queryWrapper.orderBy(StrUtil.isNotBlank(sortField), OrderConstant.ASC.equals(sortOrder), sortField);
        return queryWrapper;
    }
    @Override
    public QueryWrapper<PromptSelf> getPromptSelfQueryWrapper(PromptSelfQueryRequest promptSelfQueryRequest) {
        //提取查询字段
        Long id = promptSelfQueryRequest.getId();
        String searchText= promptSelfQueryRequest.getSearchText();
        Integer status = promptSelfQueryRequest.getStatus();
        String sortField = promptSelfQueryRequest.getSortField();
        String sortOrder = promptSelfQueryRequest.getSortOrder();
        //构造queryMapper
        QueryWrapper<PromptSelf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjUtil.isNotNull(id), "id", id);
        queryWrapper.eq(ObjUtil.isNotNull(status), "status", status);
        queryWrapper.orderBy(StrUtil.isNotBlank(sortField), OrderConstant.ASC.equals(sortOrder), sortField);
        if(StrUtil.isNotBlank(searchText)){
            queryWrapper.like("subject", searchText)
                    .or()
                    .like("role", searchText);
        }
        return queryWrapper;
    }

    @Override
    public PromptSelfVO getPromptSelfVO(PromptSelf promptSelf) {
        PromptSelfVO promptSelfVO = new PromptSelfVO();
        BeanUtil.copyProperties(promptSelf, promptSelfVO);
        return promptSelfVO;
    }

    @Override
    public List<PromptSelfVO> getPromptSelfVOList(List<PromptSelf> records) {
        return records.stream().map(this::getPromptSelfVO).toList();
    }
}




